package basic.socket.upload;

import java.io.*;
import java.net.Socket;

/**
 * @author bingo
 * @Description
 * @Date 2018/7/24
 */
public class FileServerThread implements Runnable {

    private FileInputStream fis;

    private FileOutputStream fos;

    private boolean isRunning;

    private Socket socket;



    public FileServerThread(Socket socket) {
        this.socket = socket;
        isRunning = true;
        try {
            fis = (FileInputStream) socket.getInputStream();
            fos = (FileOutputStream) socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
public void send(){
    try {
        fos.write("上传成功".getBytes());
        socket.shutdownOutput();
    } catch (IOException e) {
        isRunning = false;
        e.printStackTrace();
    }
}
    public  void  receive(){

        File file  = new File("d://upload");
        if(!file.exists()){
            System.out.println("创建文件夹。。。。");
            file.mkdir();
        }

        try {
            FileOutputStream fileOutputStream =  new FileOutputStream(new File("d://upload/one2.jpg"));
            byte[]  bytes = new byte[1024];
            int len;
            while ((len = fis.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
            }
            System.out.println("输出完毕!");

        } catch (FileNotFoundException e) {
            isRunning = false;
            e.printStackTrace();
        }catch (IOException  e) {
            isRunning = false;
            e.printStackTrace();
        }

    }
    @Override
    public void run() {
        System.out.println("isRunning"+isRunning);
        receive();
        send();

    }
}
